#include "parafluidsph.h"
#include "parafluidsphneighbor.h"

using namespace ParaFluid;

NeighborhoodReadOnlyVisitor::~NeighborhoodReadOnlyVisitor()
{
	/* does nothing */
}

NeighborhoodReadOnlyVisitor::NeighborhoodReadOnlyVisitor()
	: AtomVoxelizer::ReadOnlyVisitor()
	, _visited()
	, _atom(0)
	, _SPH(0)
{
	/* does nothing */
}

void NeighborhoodReadOnlyVisitor::visitNeighborhood(SPH & sph, const Atom & atom)
{
	_visited.clear();
	
	_atom = &atom;
	_SPH = &sph;
	
	
	Vector voxelized(_SPH->atomVoxelizer.voxelize(atom.currentPosition));
	Vector current;
	
	for(int x = -1; x <= 1; ++x)
	{
		current.x = voxelized.x + (float(x) * _SPH->smoothingDistance());
		
		for(int y = -1; y <= 1; ++y)
		{
			current.y = voxelized.y + (float(y) * _SPH->smoothingDistance());
			
			for(int z = -1; z <= 1; ++z)
			{
				current.z = voxelized.z + (float(z) * _SPH->smoothingDistance());
				
				_SPH->atomVoxelizer.acceptVisitor(*this, current);
			}
		}
	}
}

void NeighborhoodReadOnlyVisitor::visit(const Atom & atom)
{
	if(!(_visited.contains(int(&atom))))
	{
		_visited.insert(int(&atom));
		
		if(_atom->currentPosition.distanceTo(atom.currentPosition) < _SPH->smoothingDistance())
		{
			visitNeighbor(atom);
		}
	}
}
